package face.pay.web.oem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.common.user.service.UserRegisterBiz;
import face.pay.constant.RoleCode;
import face.pay.constant.UserType;
import face.pay.dao.OemInfoDao;
import face.pay.entity.OemInfo;
import face.pay.entity.UserInfo;
import face.pay.security.exception.BaseException;
import face.pay.security.util.SecurityUserUtil;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.vo.OemUserInfo;
import face.pay.web.oem.service.IOemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: zkj
 * @Date: 2019/7/3 16:32
 * @Description:oem 信息处理业务
 */
@Slf4j
@Component
@Transactional(rollbackFor = Exception.class)
public class OemServiceImpl implements IOemService {

    @Autowired
    private OemInfoDao oemInfoDao;

    @Autowired
    UserRegisterBiz userRegisterBiz;



    @Override
    public ResultMessage editOem(OemUserInfo oemUserInfo)  {

        Integer oemId = oemUserInfo.getOemId();
        if(oemId==null){
            return  this.addOem(oemUserInfo);
        }else {
            return  this.modifyOem(oemUserInfo);
        }

    }


    /**
     *
     * @param oemUserInfo
     * @return
     */
    private ResultMessage modifyOem(OemUserInfo oemUserInfo)  {

        UserInfo loginUserInfo =  SecurityUserUtil.getCurrentLoginUser();

        Integer loginUserId = loginUserInfo.getId();

        Integer oemId  =  oemUserInfo.getOemId();
        log.info("操作用户："+loginUserInfo.getUserName()+ "修改OEM");

        OemInfo oemInfo = oemUserInfo.getOemInfo();
        oemInfo.setUpdatedBy(loginUserId);

        /**
         * 检测 domain 和域名 是否存在
         *  稍后添加
         *
         */

        this.oemInfoDao.update(oemInfo);


        if(oemUserInfo.getUserId()!=null){


            UserInfo userInfo = oemUserInfo.getUserInfo();

            /**
             *   检测该oem下面 用户名已经存在
             *   稍后添加
             *
             */
            this.userRegisterBiz.updateUser(userInfo);

        }



        return ResultMessage.createSuccess();
    }




    /**
     * 添加用户
     * @param oemUserInfo
     * @return
     */
    private ResultMessage addOem(OemUserInfo oemUserInfo)  {

        UserInfo loginUserInfo =  SecurityUserUtil.getCurrentLoginUser();

        Integer userId = loginUserInfo.getId();


        log.info("操作用户："+loginUserInfo.getUserName()+ "添加OEM");

        OemInfo oemInfo = oemUserInfo.getOemInfo();

        /**
         * 检测 domain 和域名 是否存在
         *  稍后添加
         *
         */
        oemInfo.setCreatedBy(userId);
        this.oemInfoDao.insert(oemInfo);

        Integer oemId = oemInfo.getId();

        UserInfo userInfo = oemUserInfo.getUserInfo();

        userInfo.setOemId(oemId);

        userInfo.setRecommendId(userId);

        userInfo.setType(UserType.OEM);

        this.userRegisterBiz.registUserWithRole(RoleCode.ROLE_OEM_MANAGER,userInfo);

        return ResultMessage.createSuccess();
    }



    @Override
    public PageData queryPage(OemUserInfo oemUserInfo, PageParam pageParam) {

        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());

        List<OemUserInfo> oemList = this.oemInfoDao.queryOemWithUser(oemUserInfo);

        PageInfo<OemUserInfo> pageInfo = new PageInfo <OemUserInfo>(oemList);

        PageData<OemUserInfo> pageData = new PageData<OemUserInfo>(pageInfo.getTotal(),oemList);

        return pageData;
    }


    /**
     * 获取
     * @param oemId
     * @return
     * @throws BaseException
     */
    @Override
    public ResultMessage getOemUserInfo(Integer oemId) throws BaseException {

        OemUserInfo oemUserInfo =   this.oemInfoDao.getOemUserInfo(oemId);

        return  ResultMessage.createSuccess(oemUserInfo);
    }

    @Override
    public OemInfo getOemInfo(Integer oemId) throws BaseException {

        OemInfo oemUserInfo =   this.oemInfoDao.getOemInfoById(oemId);

        return oemUserInfo;
    }

    @Override
    public ResultMessage editOemInfo(OemInfo oemInfo,UserInfo loginUser) {

        oemInfo.setUpdatedBy(loginUser.getId());
        this.oemInfoDao.update(oemInfo);
        return  ResultMessage.createSuccess();

    }
}
